package com.xxx.video.mine.activitys;

import android.os.Bundle;

import androidx.recyclerview.widget.GridLayoutManager;

import com.xxx.video.R;
import com.xxx.video.commen.activitys.BaseActivity;
import com.xxx.video.commen.adapters.BaseRecycleViewAdapter;
import com.xxx.video.commen.utils.ListUtils;
import com.xxx.video.commen.utils.StatusBarUtil;
import com.xxx.video.commen.utils.StringUtils;
import com.xxx.video.commen.utils.ToastUtils;
import com.xxx.video.commen.views.recyclerviews.RefreshRecyclerView;
import com.xxx.video.dynamic.activitys.DynamicDetailsActivity;
import com.xxx.video.main.activitys.VideoDetailsActivity;
import com.xxx.video.mine.adapters.FavoriteListAdapter;
import com.xxx.video.mine.beans.FavoriteListBean;
import com.xxx.video.mine.presenters.FavoritesPresenter;
import com.xxx.video.mine.presenters.IFavoritesView;

import butterknife.BindView;


public class FavoritesActivity extends BaseActivity<FavoritesPresenter> implements IFavoritesView,
        BaseRecycleViewAdapter.OnItemClickListener<FavoriteListBean.PageBean.ListBean>, RefreshRecyclerView.OnRecyclerViewRefreshOrLoadMoreListener {
    @BindView(R.id.rcv_my_collection_list)
    RefreshRecyclerView rcvMyCollectionList;
    /**
     * page
     */
    private int page = 1;
    private FavoriteListAdapter mFavoriteListAdapter;

    @Override
    public void initToolbarData() {
        super.initToolbarData();
        showActionBar();
        StatusBarUtil.getInstance().setStatusBarTransparent(this, true);
        setIsShowActionbarLine(true);
        setActionBarTitle(R.string.title_favorite_activity);
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_favorites;
    }

    @Override
    protected FavoritesPresenter createPresenter() {
        return new FavoritesPresenter(this, this);
    }

    @Override
    public void initData(Bundle extras) {
        getPresenter().collectionList(page);
    }

    @Override
    public void showRootLayoutStatus(int status) {
        showLayoutStatus(status);
    }

    @Override
    public void initData2View(FavoriteListBean successOb) {
        showLayoutStatus(1);
        rcvMyCollectionList.resetState();
        if (mFavoriteListAdapter == null) {
            rcvMyCollectionList.setLayoutManager(new GridLayoutManager(this, 2));
            mFavoriteListAdapter = new FavoriteListAdapter(this);
            rcvMyCollectionList.setAdapter(mFavoriteListAdapter);
            mFavoriteListAdapter.setOnItemClickListener(this);
            rcvMyCollectionList.setOnRecyclerViewRefreshOrLoadMoreListener(this);
        }
        if (page == 1) {
            mFavoriteListAdapter.setData(successOb.page.list);
            if (ListUtils.isEmpty(successOb.page.list)) {
                showLayoutStatus(2);
            }
        } else {
            mFavoriteListAdapter.addData(successOb.page.list);
        }
    }

    @Override
    public void unCollectionSuccess(int position) {
        if (mFavoriteListAdapter != null) {
            mFavoriteListAdapter.removeDataByPosition(position);
        }
        ToastUtils.showSuccess(StringUtils.getInstance().getString(R.string.tip_un_collection_success));
    }

    @Override
    public void startVideoDetailsActivity(String id) {
        Bundle bundle = new Bundle();
        bundle.putString("Id", id);
        intent2Activity(VideoDetailsActivity.class, bundle);
    }

    @Override
    public void startDynamicDetailsActivity(String dynamicId) {
        Bundle bundle = new Bundle();
        bundle.putString("Id", dynamicId);
        intent2Activity(DynamicDetailsActivity.class, bundle);
    }

    @Override
    public void onRecyclerviewRefresh() {
        page = 1;
        getPresenter().collectionList(page);
    }

    @Override
    public void onRecyclerviewLoadMore() {
        page++;
        getPresenter().collectionList(page);
    }

    @Override
    public void onItemClick(FavoriteListBean.PageBean.ListBean listBean, int position, int type) {
        if (type == 0) {
            if (listBean.fType == 0) {
                startVideoDetailsActivity(listBean.fBussId);
            } else if (listBean.fType == 1) {
                startDynamicDetailsActivity(listBean.fBussId);
            }
        } else if (type == 1) {
            getPresenter().deleteCollection(listBean.fId, position);
        }
    }
}
